<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: discuz_block.php 27449 2012-02-01 05:32:35Z zhangguosheng $
 */
include_once("../../config.php");
require_once(APP_PATH . "/Common/yzs1013_pldr.php");

class collect_tongyong extends collect_base
{
    public static function getPageContent($url,$gather_type=1) {
            jsstart("top");	//计时开始
			$news=new zqnews($url);
			$nr=$news->nr;
			$rs=$news->rs;
			$jj=$news->jj;
			$pic=$news->pic; 
		
			$pagehtml = $nr;      
            $pagehtml = preg_replace("/　/",'',$pagehtml);   
			//$pagehtml=iconv("GB2312","UTF-8//IGNORE",$pagehtml);

			
            //charset   
            if($pageinfo[charset] == '' ) {  
                preg_match('@<meta.+charset=([\w\-]+)[^>]*>@i',$pagehtml,$matches);  
                $pageinfo[charset] = trim($matches[1]);  
            }  
            //desctiption  
            preg_match('@<meta\s+name=\"*description\"*\s+content\s*=\s*([^/>]+)/*>@i',$pagehtml,$matches);  
 
            $desc = trim($matches[1]);  
            $pageinfo[description] = str_replace("\"", '',$desc);  
              
  
            preg_match('@<meta\s+name=\"*keywords\"*\s+content\s*=\s*([^/>]+)/*>@i',$pagehtml,$matches);  
            $keywords = trim($matches[1]); 
			
			preg_match_all("/<title>[^<>]*[\-_]([^<>\-_]*(?:网|社区|论坛))[^<>]*<\/title>/Uisx",$pagehtml,$nn); 
			$wangming=$nn[1][0];
			if(trim($wangming)==""){
			preg_match('/<span\s+id=\"media_name\">(.*)<\/span>/Uisx',$pagehtml,$nn); 
			$wangming=trim(strip_tags($nn[1]));
			}
			if(trim($wangming)==""){
			preg_match("/<link\s+rel=\'archives\'\s+title=\'([^\-\_]*网|社区|论坛)\'/Uisx",$pagehtml,$nn); 
			$wangming=trim(strip_tags($nn[1]));
			}			
			if(trim($wangming)==""){
			$wanming=tongx("来源",$pagehtml);
			}				
            $pageinfo['keywords'] = str_replace("\"", '',$keywords);  
            $pageinfo['subject'] =$pageinfo['title'] = cltitle($news->getbt());  
			$pageinfo['views']=tongx("访问数,访问,点击数,浏览数,点击,浏览,人气,查看,阅读",$pagehtml,1);
            if(trim($pageinfo['views'])==""){
            preg_match("/第([0-9]+)个(阅读|查看)/Uisx",$pagehtml,$nn); 
            $pageinfo['views']=trim($nn[1]);
            }
			$pageinfo['replies']=tongx("回复数,回复",$pagehtml,1);

                		
			$pageinfo['dateline']=tongx("发表时间,发表日期,发布日期,发布日期",$pagehtml);
            
            if($pageinfo['dateline']==""){
                preg_match_all("/发表于\s*<span\s*title=\"(\b[0-9]{2,4}\-[0-9]{1,2}\-[0-9]{1,2}[0-9\-\s\:]*)\">/Uisx",$pagehtml,$nn);
                   $pageinfo['dateline']=$nn[1][0];
            }			

            if($pageinfo['dateline']==""){
			    preg_match_all("/[0-9]{2,4}年[0-9]{1,2}月[0-9]{1,2}日\s*[0-9]{1,2}\s*\:\s*[0-9]{1,2}?/Uisx",$pagehtml,$nn);
			   	$pageinfo['dateline']=$nn[0][0];
			}
			if($pageinfo['dateline']==""){
			    preg_match_all("/\b[0-9]{2,4}\-[0-9]{1,2}\-[0-9]{1,2}[0-9\-\s\:]*/isx",$pagehtml,$nn);
			   	$pageinfo['dateline']=$nn[0][0];
			}			
			
		
			$pageinfo['laiyuan']=tongx("来源",$pagehtml);
			$pageinfo['s_sitename']=tongx("application-name",$pagehtml);
			
			$pageinfo['author']=tongx("编辑,责编,作者",$pagehtml,2);
			if($pageinfo['author']==""){
			preg_match("/<META\s+name=\"author\"\s+content=\"([\x{4e00}-\x{9fa5}A-Za-z0-9_]+)\">/Uuisx",$pagehtml,$nn); 
			$pageinfo['author']=trim(strip_tags($nn[1]));
			}
			
			if($pageinfo['author']==""){
			preg_match("/<span\s+id=\"blognamespan\">([\x{4e00}-\x{9fa5}A-Za-z0-9_]+)<\/span>/Uuisx",$pagehtml,$nn); 
			$pageinfo['author']=trim(strip_tags($nn[1]));
			}			
			if($pageinfo['author']==""){
			preg_match("/本主题由(.*)于/Uuisx",$pagehtml,$nn); 
			$pageinfo['author']=trim(strip_tags($nn[1]));
			}
	
				
			if($pageinfo['plate']==""){
			preg_match("/<META\s+name=\"source\"\s+content=\"([\x{4e00}-\x{9fa5}A-Za-z0-9_]+)\">/Uuisx",$pagehtml,$nn); 
			$pageinfo['plate']=trim(strip_tags($nn[1]));
			}
			if($pageinfo['plate']==""){
			preg_match_all("/<title>[^<>]*[\-_]([^<>]*)[\-_][^<>]*<\/title>/Uisx",$pagehtml,$nn); 
            $pageinfo['plate']=$nn[1][0];
			}
			if(trim($pageinfo['plate'])==""){
			preg_match("/<a\s*href=\"[^<>]*\">([^<>]*)<\/a>\s*\-?\->\s*正文/Uisx",$pagehtml,$nn); 
			$pageinfo['plate']=trim(strip_tags($nn[1]));
			}           
			if(trim($pageinfo['plate'])==""){
            $pageinfo['plate']=tongx("版块,来源",$pagehtml);
			}
			
            if(!preg_match("/[\x{4e00}-\x{9fa5}]+/Uuisx",$pageinfo['plate'])){
			$pageinfo['plate']="";
			}
			$pageinfo['plate']=$wangming."-".mustzw(clwz($pageinfo['plate']));
			$pageinfo['plate']=preg_replace("/^网\-?/isx","",$pageinfo['plate']);
            $pageinfo['plate']=trim($pageinfo['plate']);

			switch($gather_type){
				case 2:
			    $pageinfo['message'] = $news->getbbs();
				break;
				case 3:
			    $pageinfo['message'] = $news->getflv();
				break;
				default:
			    $pageinfo['message'] = $news->getnr();
			}


			if(!$news->secexe&&$gather_type!=2){
					$pageno=0;
                    $nextpagerem=$news->nextpagerem;

                    if(count($nextpagerem)>0){
							for($i=0;$i<count($nextpagerem);$i++){
								do{

									preg_match_all("/".trim($nextpagerem[$i])."/Uisx",$news->ytempnr,$mm);

									if(count($mm[1])<1||preg_match("/javascript/Uisx",$mm[1][0])) break;
									$mm[1][0]=quanurl($mm[1][0],$url);
									
									if($mm[1][0]==$oldurl){
										break;
									}else{
										$oldurl=$mm[1][0];
									}

									$news=new zqnews($mm[1][0]);
									$pageinfo['message'].=$news->getnr();
									$pageno++;
									if($pageno>40) break;
								}while(1==1);
							}
					}else{
						$news->nr=(!isutf8($news->nr))?u($news->nr):$news->nr;
						if($news->bug==3){
							   echo $news->nextpagere."\r\n";
							   echo "<a[^<>]*href=[\'\"]([^<>\'\"]*?)[^<>]*>(?:<\/?(?:span|strong|b|font|\[)[^<>]*>|\s)*(下一页|下一页|下页|后一页|后页|Next|\&gt\;".$news->nextpagere.g($news->nextpagere).")(?:<\/?(?:span|strong|b|font|\[)[^<>]*>|\s|\&nbsp\;|\&gt\;|>)*<\/a>";
							   $fn=fopen("d:/temp.txt","w");fwrite($fn,$news->url."\r\n".$news->nr);fclose($fn);
						}	
                      
							if(preg_match_all("/<a[^<>]*href=[\'\"]([^<>\'\"]*?)[^<>]*>(?:<\/?(?:span|strong|b|font)[^<>]*>|\s|\[)*(下一页|下一页|下页|后一页|后页|Next|\&gt\;".$news->nextpagere.g($news->nextpagere).")(?:<\/?(?:span|strong|b|font)[^<>]*>|\s|\&nbsp\;|\&gt\;|>|\])*<\/a>/Uisx",$news->nr,$mm)){
							    					
								$oldurl="";					
								while(preg_match_all("/<a[^<>]*href=[\'\"]([^<>'\"]*?)[^<>]*>(?:<\/?(?:span|strong|b|font)[^<>]*>|\s|\[)*(下一页|下一页|下页|后一页|后页|Next|\&gt\;".$news->nextpagere.g($news->nextpagere).")(?:<\/?(?:span|strong|b|font)[^<>]*>|\s|\&nbsp\;|\&gt\;|>|\])*<\/a>/Uisx",$news->nr,$mm)){
                                     
                                    if(ceil(js("top"))>30) break;
									$mm[1][0]=preg_replace("/http\:\/(?!\/)/Uisx","/",$mm[1][0]);
									if(preg_match("/javascript/Uisx",$mm[1][0])) break;
									
									$mm[1][0]=quanurl($mm[1][0],$url);
									
									if($mm[1][0]==$oldurl||(preg_match("/(_|\-)1\.htm/Uisx",$mm[1][0])&&$oldurl!="")){
										break;
									}else{
										$oldurl=$mm[1][0];
									}

									if($news->bug==3){
										if(!preg_match("/\?/",$mm[1][0])){
									    $mm[1][0]=$mm[1][0]."?bug=3";
										}else{
                                        $mm[1][0]=$mm[1][0]."&bug=2";
										}
									}
									if($news->bug==3) echo "\r\n\r\npage:{$pageno}--------------".$mm[1][0]."------------------\r\n\r\n";//
									$news=new zqnews($mm[1][0]);
									$pageinfo['message'].="\r\n".$news->getnr();
									$pageno++;
									$news->nr=(!isutf8($news->nr))?u($news->nr):$news->nr;
									if($oldnr==$news->nr){
										break;
									}else{
										$oldnr=$news->nr;
									}
									if($pageno>25) break;
									
								}
								
						   }
					}
			}
			$pageinfo['plate']=preg_replace("/\(|\)/Uisx","",$pageinfo['plate']);
			$pageinfo['plate']=preg_replace("/(\-$|^\-)/Uisx","",$pageinfo['plate']);
			$pageinfo['from']=$pageinfo['plate'];
			$pageinfo['status']=(strlen($pageinfo['message'])<100)?'error':'ok';
			$pageinfo['s_url']=$url;

            if($rs['puhuo']!=""){
                   $cc=preg_split("/\|\|\||$/m",$rs['puhuo']);
                   for($i=0;$i<count($cc);$i++){
						if(preg_match("/\:([a-z_0-9]+)(?=$|\:)/Uisx",trim($cc[$i]),$mm4)){
                              $cc[$i]=preg_replace("/\:[a-z_0-9]+(?=$|\:)/Uisx","",trim($cc[$i]));
                              preg_match_all("/".trim($cc[$i])."/Uisx",$news->nr,${"nn".$mm4[1]});
							  $tempnn=${"nn".$mm4[1]};
							  if(trim($tempnn[1][0])!=""){
								  $pageinfo[$mm4[1]]=$tempnn[1][0];
								  $pageinfo['message'].=$mm4[1].":".unicode_decode2(trim($tempnn[1][0]))."\r\n";
							  }
                        }
				   

					if(preg_match("/\>\>([a-z_0-9]+)(?=$|\:)/Uisx",trim($cc[$i]),$mm4)){
						
						  $cc[$i]=preg_replace("/\>\>[a-z_0-9]+(?=$|\:)/Uisx","",trim($cc[$i]));
						  preg_match_all("/".trim($cc[$i])."/Uisx",$news->nr,${"nn".$mm4[1]});
						  $tempnn=${"nn".$mm4[1]};
						  $arr=sjson_decode($tempnn[1][0]);
						  foreach($arr as $k=>$v){
							  if(is_array($v)){
								  $pageinfo[$k]=json_encode($v);
							  }else{
								  $pageinfo[$k]=$v;
							  }
							  $pageinfo['message']=trim($pageinfo['message'])."\r\n".trim($k)."='".$pageinfo[$k]."'\r\n[align=center][/align]";
						  }
					}
					}
			}
            return $pageinfo;
    }  
}

function class2array($array){
	if(is_object($array)){
		$array = (array)$array;
	}
	if(is_array($array)){
		foreach($array as $key=>$value){
			$array[$key] = class2array($value);
		}
	}
	return $array;
}

function mj2array($array){
  $newarr=array();
  if(!empty($array)){
  foreach($array as $k=>$v){
	 $array[$k][key]=$k;
     $newarr[]=$array[$k];
  }}
  return $newarr;
}

function sjson_decode($nr){
    $nr=preg_replace("/^[^\{\}]*?\{(.*)\}\;?/isx","(\\1)",$nr);
    $nr = preg_replace("/\]/",")",$nr);
    $nr = preg_replace("/\[/","(",$nr);
    $nr = preg_replace("/\(/","array(",$nr);
	$nr = preg_replace("/\"\:/","\"=>",$nr);
	$nr = preg_replace("/\{/","array(",$nr);
	$nr = preg_replace("/\}/",")",$nr);
    eval("\$arr=".unicode_decode1($nr).";");
  return $arr;
}
?>